package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation("套餐分页")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("菜品分页查询:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @CacheEvict(value = "cache:setmeal", key = "#setmealDTO.categoryId")
    @ApiOperation("套餐新增")
    public Result<String> saveSetmealWhithDish(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveSetmealWhithDish(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(value = "cache:setmeal", allEntries = true)
    @ApiOperation("套餐删除")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("套餐批量删除：{}", ids);
        setmealService.batchDelete(ids);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "cache:setmeal", allEntries = true)
    @ApiOperation("套餐禁用启用")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
    }

    @PutMapping
    @CacheEvict(value = "cache:setmeal", allEntries = true)
    @ApiOperation("套餐修改")
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("套餐详情")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

}
